#coding:utf-8
'''
create on 2017-04-20

@author: sandy
'''

import logging
import HallManager
import ClientBuild
from pycommon import Const
import Center
import CenterBuild
import Game
import ClientFactory
import TimeCheck

USER_LEAVE_LOGOUT       = 0
USER_LEAVE_CON_LOST     = 1
USER_LEAVE_KICK         = 2

class UserManager(object):
    
    def __init__(self):
        self.usermap = {}
        
    def start(self):
        pass
    
    def stop(self):
        pass
    
    def getUser(self, uid):
        return self.usermap.get(uid)

    def onCheckUserSuc(self,uid,session,sex,photo,nickname):
        olduser = self.getUser(uid)
        if olduser:
            olduser.setUserBaseInfo(uid,session,sex,photo,nickname)
        else:
            newuser = Game.instance.createUser(uid)
            newuser.setUserBaseInfo(uid,session,sex,photo,nickname)
            self.usermap[uid] = newuser
            Game.instance.onCheckUserSuc(uid)

    
    def onUserEnter(self,uid,hallId,tableId,tableIdStr):
        hall = HallManager.instance.getHall(hallId)
        code = hall.userEnter(uid)
        if code == Const.ERR_OK:
           table = hall.getTable(tableId)
           code = table.userEnter(uid)
        return code

    def onUserLeave(self, uid, reason): 
        logging.debug(u"onUserLeave(), uid=%d, reason=%d", uid, reason)
        user = self.getUser(uid)
        hallId = user.getHallId()
        hall = HallManager.instance.getHall(hallId)
        if hall:
            hall.userLeave(user, reason)
            self.usermap.pop(uid)
            Center.instance.sendPacket(CenterBuild.userLogout(uid))
        else:
            logging.debug("hallManager onUserLeave uid=%d hallId=%d hallIsNone",uid,hallId)

    def onUserLoginResult(self,uid,code):
        logging.debug(u"onUserLoginResult() uid=%d err=%s", uid, code)
        client = ClientFactory.instance.getClientSrvByUid(uid)
        if Const.ERR_OK != code:
            client.sendPacket(ClientBuild.clientLogin(code))
            TimeCheck.instance.addLoginFail(uid)
            client.goToClose()
            self.usermap.pop(uid)
            return
        client.sendPacket(ClientBuild.clientLogin(Const.ERR_OK))
        client.onLoginSuc()
    
        
    def kickUser(self, uid, ecode):
        kuser = self.getUser(uid)
        if not kuser:
            logging.warn(u"kickUser() getUser() return None, uid=%d", uid)
            Center.instance.sendPacket(CenterBuild.userLogout(uid))         
            return
        kuser.sendPacket(ClientBuild.kickUser())
        kuser.kick()
        self.onUserLeave(kuser, Const.USER_LEAVE_KICK) 
        
    def broadcast(self, pkt):
        for user in self.usermap.values():
            user.sendPacket(pkt)
            
    def kickAll(self):
        uidlist = self.usermap.keys()[:]
        for uid in uidlist:
            self.kickUser(uid, Const.ERR_KICK_SYS_MAINTAIN)
            
    def broadcastGameMsg(self, halllevel, msgtype, msg):
        center.instance.sendPacket(CenterBuild.broadcastGameMsg(halllevel, msgtype, msg))              

instance = UserManager()

if __name__ == '__main__':
    pass